<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends  Controller
{
    public function _initialize()
    {
        header('Content-Type:text/html; charset=utf-8');
        if (Session::get('user')==null){
            $this->redirect('index/login/login');exit;
        }
        //判断用户是否登录
        //判断权限
        //登录有效期
    }

    //数据概览
    public function index()
    {
        $partners_and_tools = Db::name('partners_and_tools');
        $data = $partners_and_tools->where("id=1")->field('store_num,merchant_num,cashier_num,terminal_num,time,man_num,men_num')->find();
        $today = date("Y-m-d",time());
        if ($data['time']<$today){
            $data['store_num'] = (string)(round($data['store_num']*1)+rand(500,1000));
            $data['merchant_num'] = (string)round($data['store_num']/2.6);
            $data['cashier_num'] = (string)round($data['store_num']*2.8);
            $data['terminal_num'] = (string)round($data['cashier_num']*1.2);
            $data['man_num'] = (string)round($data['cashier_num']/3.2);//男收银员数量
            $data['men_num'] = (string)($data['cashier_num']*1-$data['man_num']*1);//女收银员数量
            $data['time'] = (string)($today);
            $partners_and_tools->where("id=1")->update($data);
        }
        $this->assign($data);
        return $this->fetch('data_overview');
    }

    //全屏概览图
    public function full_data_overview(){
        $partners_and_tools = Db::name('partners_and_tools');
        $data = $partners_and_tools->where("id=1")->field('store_num,merchant_num,cashier_num,terminal_num,time,man_num,men_num')->find();
        $today = date("Y-m-d",time());
        if ($data['time']<$today){
            $data['store_num'] = (string)(round($data['store_num']*1)+rand(500,1000));
            $data['merchant_num'] = (string)round($data['store_num']/2.6);
            $data['cashier_num'] = (string)round($data['store_num']*2.8);
            $data['terminal_num'] = (string)round($data['cashier_num']*1.2);
            $data['man_num'] = (string)round($data['cashier_num']/3.2);//男收银员数量
            $data['men_num'] = (string)($data['cashier_num']*1-$data['man_num']*1);//女收银员数量
            $data['time'] = (string)($today);
            $partners_and_tools->where("id=1")->update($data);
        }
        $this->assign($data);
        return $this->fetch('full_data_overview');
    }

    //实时交易数据
    public function real_time()
    {
        return $this->fetch('real_time');
    }

    //全屏实时交易图
    public function full_real_time(){
        return $this->fetch('full_real_time');
    }

    //区域门店数s
    public function regional_store()
    {
        $sheng = '';
        //得到系统的年月
        $tmp_date = date("Ym");
        //切割出年份
        $tmp_year = substr($tmp_date, 0, 4);
        //切割出月份
        $tmp_mon = substr($tmp_date, 4, 2);
        $tmp_forwardmonth = mktime(0, 0, 0, $tmp_mon - 1, 1, $tmp_year);
        //得到当前月的上一个月
        $date1 = $fm_forward_month = date("Y-m", $tmp_forwardmonth);
        if (!isset($_GET['date1'])) {
            if (session('date1')) {
                $date1 = session('date1');
            }
        } else {
            $date1 = $_GET['date1'];
            session('date1', $date1);
        }

        if (!isset($_GET['sheng'])) {
            if (session('sheng')) {
                $sheng = session('sheng');
            }
        } else {
            $sheng = $_GET['sheng'];
            if ($_GET['sheng'] == '') {
                Session::delete('sheng');
            }
            session('sheng', $sheng);
        }

        $mp = Db::name('month_province');
        $data = $mp->where("date='{$date1}'")->select();
        $arr = [];
        $count = 0;
        $page = '';
        $shengArr = [];
        if (!empty($data)) {
            $data1 = json_decode($data[0]['data'], true);
            foreach ($data1 as $key => $value) {
                $shengArr[] = $key;
                $count = $count + $value;
            }
            if (!in_array($sheng, $shengArr)) {
                $sheng = '';
            } else {
                $count = $data1[$sheng];
            }
//            var_dump($count);exit;
            $limit = 15;
            //如果没传页数
            if (isset($_GET['page'])) {
                if ($_GET['page'] == ceil($count / 15)) {
                    if ($count % 15 == 0) {
                        $limit = 15;
                    } else {
                        $limit = $count % 15;
                    }
                }
            }

            $rs = Db::name('regional_store');
            if ($sheng == '') {
                $ids1 = [];
                foreach ($shengArr as $value) {
                    $ids2 = $rs->where("province='{$value}'")->column("id");
                    $ids1 = array_merge($ids1, $ids2);
                }
                $useId = [];
                for ($i = 0; $i < $limit; $i++) {
                    $useId[] = $ids1[rand(0, count($ids1) - 1)];
                }
                foreach ($useId as $value) {
                    $arr[] = $rs->where("id=$value")->select()[0];
                }

            } else {
                $ids1 = $rs->where("province='{$sheng}'")->column("id");
                $useId = [];
                for ($i = 0; $i < $limit; $i++) {
                    $useId[] = $ids1[rand(0, count($ids1) - 1)];
                }
                foreach ($useId as $value) {
                    $arr[] = $rs->where("id=$value")->select()[0];
                }

            }

            $res = $rs->paginate(15,(int)$count);
            // 获取分页显示
            $page = $res->render();
//            var_dump($page);exit;
        }

        // 模板变量赋值
        $this->assign('shengArr', $shengArr);
        $this->assign('sheng', $sheng);
        $this->assign('date1', $date1);
        $this->assign('count', $count);
        $this->assign('res', $arr);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch('regional_store');
    }

    // 2015-2018门店数量趋势图
    public function stores_show()
    {
        $ds = Db::name('date_stores');
        $data = $ds->select();
        for ($i = 0; $i < count($data); $i++) {
            if ($i == 0) {
                $oneDate[] = $data[$i]['date'];
                $oneStores[] = $data[$i]['stores'];
            } elseif ($i > 0 && $i <= 12) {
                $twoDate[] = $data[$i]['date'];
                $twoStores[] = $data[$i]['stores'];
            } elseif ($i > 12 && $i <= 24) {
                $threeDate[] = $data[$i]['date'];
                $threeStores[] = $data[$i]['stores'];
            } elseif ($i > 24 && $i <= 37) {
                $fourDate[] = $data[$i]['date'];
                $fourStores[] = $data[$i]['stores'];
            }
        }
        foreach ($data as $value) {
            $date[] = $value['date'];
            $stores[] = $value['stores'];
        }

        $this->assign('oneDate', $oneDate);
        $this->assign('oneStores', $oneStores);
        $this->assign('twoDate', $twoDate);
        $this->assign('twoStores', $twoStores);
        $this->assign('threeDate', $threeDate);
        $this->assign('threeStores', $threeStores);
        $this->assign('fourDate', $fourDate);
        $this->assign('fourStores', $fourStores);

        $this->assign('date', $date);
        $this->assign('stores', $stores);
        return $this->fetch('stores_show');
    }

    // 获取每月的门店数量 用在区域门店趋势页面
    public function date_stores()
    {
        $ds = Db::name('date_stores');
        $data = $ds->select();
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    //会员数据
    public function member_data (){
        $po = Db::name('php_get_storename1');
        $od = Db::name('order_data');
        $count = (int)$od->order("id desc")->value('member_num');

        $number = rand(1,2250);
        //如果没传页数
        if (!isset($_GET['page'])){
            $res = $po->paginate(15,$count);
            $arr = $po->where("id>=$number")->limit(15)->select();
        }else{
            $res = $po->paginate(15,$count);

            $var = trim($_GET['page']);
            $len = strlen($var)-1;
            $num = $var{$len};//页数最后一个数字

            $limit = 15;
            if ($_GET['page'] == ceil($count / 15)) {
                if ($count % 15 == 0) {
                    $limit = 15;
                } else {
                    $limit = $count % 15;
                }
            }
            $arr = $po->where("id>=$number")->limit($limit)->select();

        }
        foreach($arr as $key=>$value){
            if(substr($value['order_time'],5,2)=='05' || substr($value['order_time'],5,2)=='03'){
                $arr[$key]['order_time'] = '2018-08'.substr($value['order_time'],7);
                $arr[$key]['pay_time'] = '2018-08'.substr($value['pay_time'],7);
                $arr[$key]['zhuce_time'] = '2018-0'.rand(4,7).'-'.rand(11,29).substr($value['zhuce_time'],10);
            }
            if(substr($value['order_time'],8,2)=='31'){
                $arr[$key]['order_time'] = '2018-08-30'.substr($value['order_time'],10);
                $arr[$key]['pay_time'] = '2018-08-30'.substr($value['pay_time'],10);
                $arr[$key]['zhuce_time'] = '2018-0'.rand(5,8).'-'.rand(11,29).substr($value['zhuce_time'],10);
            }
        }
        $page = $res->render();
        $this->assign('count', $count);
        $this->assign('res', $arr);
        $this->assign('page', $page);
        return $this->fetch('member_data');
    }

    //全屏会员数据
    public function full_member_data (){
        $po = Db::name('php_get_storename1');
        $od = Db::name('order_data');
        $count = (int)$od->order("id desc")->value('member_num');

        $number = rand(1,2250);
        //如果没传页数
        if (!isset($_GET['page'])){
            $res = $po->paginate(15,$count);
            $arr = $po->where("id>=$number")->limit(15)->select();
        }else{
            $res = $po->paginate(15,$count);

            $var = trim($_GET['page']);
            $len = strlen($var)-1;
            $num = $var{$len};//页数最后一个数字

            $limit = 15;
            if ($_GET['page'] == ceil($count / 15)) {
                if ($count % 15 == 0) {
                    $limit = 15;
                } else {
                    $limit = $count % 15;
                }
            }
            $arr = $po->where("id>=$number")->limit($limit)->select();

        }
        foreach($arr as $key=>$value){
             if(substr($value['order_time'],5,2)=='05' || substr($value['order_time'],5,2)=='03'){
                $arr[$key]['order_time'] = '2018-08'.substr($value['order_time'],7);
                $arr[$key]['pay_time'] = '2018-08'.substr($value['pay_time'],7);
                $arr[$key]['zhuce_time'] = '2018-0'.rand(4,7).'-'.rand(11,29).substr($value['zhuce_time'],10);
            }
            if(substr($value['order_time'],8,2)=='31'){
                $arr[$key]['order_time'] = '2018-08-30'.substr($value['order_time'],10);
                $arr[$key]['pay_time'] = '2018-08-30'.substr($value['pay_time'],10);
                $arr[$key]['zhuce_time'] = '2018-0'.rand(5,8).'-'.rand(11,29).substr($value['zhuce_time'],10);
            }
        }
        $page = $res->render();
        $this->assign('count', $count);
        $this->assign('res', $arr);
        $this->assign('page', $page);
        return $this->fetch('full_member_data');
    }

    //订单数据
    public function transaction_data (){
        //得到系统的年月
        $tmp_date = date("Ym");
        //切割出年份
        $tmp_year = substr($tmp_date, 0, 4);
        //切割出月份
        $tmp_mon = substr($tmp_date, 4, 2);
        $tmp_forwardmonth = mktime(0, 0, 0, $tmp_mon - 1, 1, $tmp_year);
        //得到当前月的上一个月
        $date = $fm_forward_month = date("Y-m", $tmp_forwardmonth);
        if (!isset($_GET['date'])) {
            if (session('date')) {
                $date = session('date');
            }
        } else {
            $date = $_GET['date'];
            session('date', $date);
        }

        // 初始化页面显示数据
        $this_month_order_price = 0;// 该月交易金额
        $all_order_price = 0; // 今年累计交易金额
        $all_order_num = 0; // 今年累计交易笔数
        $arr = []; // 遍历的数据
        $page = ''; // 页数
        $count = 0; // 总数量 交易笔数
        $limit = 15; // 每页显示个数

        $po = Db::name('php_get_storename1');
        $od = Db::name('order_data');
        $res = $od->where("month='$date'")->select();
        if(!empty($res)){
            $date1 = substr($date,0,4).'-01';
            $order_data = $od->where("month>='$date1' and month<='$date'")->select();
            $all_order_price = 0;
            foreach ($order_data as $value){
                $this_month_order_price = $value['order_price'];
                $count = (int)$value['order_num'];
                $all_order_price = $all_order_price*1+$value['order_price']*1;
                $all_order_num = $all_order_num*1+$count;
            }

            $number = rand(1,2250);
            //如果没传页数
            if (!isset($_GET['page'])){
                $res = $po->paginate(15,$count);
                $arr = $po->where("id>=$number")->limit(15)->select();
            }else{
                $res = $po->paginate(15,$count);

                //如果传页数了
                if ($_GET['page'] == ceil($count / 15)) {
                    if ($count % 15 == 0) {
                        $limit = 15;
                    } else {
                        $limit = $count % 15;
                    }
                }
                $arr = $po->where("id>=$number")->limit($limit)->select();
            }
            $page = $res->render();
        }
        foreach($arr as $key=>$value){
            $arr[$key]['order_time'] = $date.substr($value['order_time'],7);
            $arr[$key]['pay_time'] = $date.substr($value['pay_time'],7);
            if(substr($value['order_time'],8,2)=='31'){
                $arr[$key]['order_time'] = $date.'-30'.substr($value['order_time'],10);
                $arr[$key]['pay_time'] = $date.'-30'.substr($value['pay_time'],10);
            }
        }

        $this->assign('date', $date);
        $this->assign('count', $count);
        $this->assign('this_month_order_price', $this_month_order_price);
        $this->assign('all_order_price', $all_order_price);
        $this->assign('all_order_num', $all_order_num);
        $this->assign('res', $arr);
        $this->assign('page', $page);
        return $this->fetch('transaction_data');
    }

    //全屏订单数据
    public function full_transaction_data (){
        //得到系统的年月
        $tmp_date = date("Ym");
        //切割出年份
        $tmp_year = substr($tmp_date, 0, 4);
        //切割出月份
        $tmp_mon = substr($tmp_date, 4, 2);
        $tmp_forwardmonth = mktime(0, 0, 0, $tmp_mon - 1, 1, $tmp_year);
        //得到当前月的上一个月
        $date = $fm_forward_month = date("Y-m", $tmp_forwardmonth);
        if (!isset($_GET['date'])) {
            if (session('date')) {
                $date = session('date');
            }
        } else {
            $date = $_GET['date'];
            session('date', $date);
        }

        // 初始化页面显示数据
        $this_month_order_price = 0;// 该月交易金额
        $all_order_price = 0; // 今年累计交易金额
        $all_order_num = 0; // 今年累计交易笔数
        $arr = []; // 遍历的数据
        $page = ''; // 页数
        $count = 0; // 总数量 交易笔数
        $limit = 15; // 每页显示个数

        $po = Db::name('php_get_storename1');
        $od = Db::name('order_data');
        $res = $od->where("month='$date'")->select();
        if(!empty($res)){
            $date1 = substr($date,0,4).'-01';
            $order_data = $od->where("month>='$date1' and month<='$date'")->select();
            $all_order_price = 0;
            foreach ($order_data as $value){
                $this_month_order_price = $value['order_price'];
                $count = (int)$value['order_num'];
                $all_order_price = $all_order_price*1+$value['order_price']*1;
                $all_order_num = $all_order_num*1+$count;
            }

            $number = rand(1,2250);
            //如果没传页数
            if (!isset($_GET['page'])){
                $res = $po->paginate(15,$count);
                $arr = $po->where("id>=$number")->limit(15)->select();
            }else{
                $res = $po->paginate(15,$count);

                //如果传页数了
                if ($_GET['page'] == ceil($count / 15)) {
                    if ($count % 15 == 0) {
                        $limit = 15;
                    } else {
                        $limit = $count % 15;
                    }
                }
                $arr = $po->where("id>=$number")->limit($limit)->select();
            }
            $page = $res->render();
        }
        foreach($arr as $key=>$value){
            $arr[$key]['order_time'] = $date.substr($value['order_time'],7);
            $arr[$key]['pay_time'] = $date.substr($value['pay_time'],7);
            if(substr($value['order_time'],8,2)=='31'){
                $arr[$key]['order_time'] = $date.'-30'.substr($value['order_time'],10);
                $arr[$key]['pay_time'] = $date.'-30'.substr($value['pay_time'],10);
            }
        }

        $this->assign('date', $date);
        $this->assign('count', $count);
        $this->assign('this_month_order_price', $this_month_order_price);
        $this->assign('all_order_price', $all_order_price);
        $this->assign('all_order_num', $all_order_num);
        $this->assign('res', $arr);
        $this->assign('page', $page);
        return $this->fetch('full_transaction_data');
    }

    //返回过去六个月的交易信息
    public function past_six_months(){
        $od = Db::name('order_data');
        $data = $od->order('id desc')->limit(6)->select();
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    //支付渠道使用率
    public function channel_proportion(){
        $channel_proportion = Db::name('channel_proportion');
        $data = $channel_proportion->where("id=1")->field('alipay,wxpay,unionpay,wing_payment,time')->find();
        $today = date("Y-m-d",time());
        if ($data['time']<$today){
            $data['alipay'] = $_POST['alipay'];
            $data['wxpay'] = $_POST['wxpay'];
            $data['unionpay'] = $_POST['unionpay'];
            $data['wing_payment'] = $_POST['wing_payment'];
            $data['time'] = $today;
            $channel_proportion->where("id=1")->update($data);
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    //炫生活服务平台行业使用占比
    public function industry_proportion(){
        $industry_proportion = Db::name('industry_proportion');
        $data = $industry_proportion->where("id=1")->field('restaurant,fast_selling,living_service_furniture,medical_care,other,time')->find();
        $today = date("Y-m-d",time());
        if ($data['time']<$today){
            $data['restaurant'] = $_POST['restaurant'];
            $data['fast_selling'] = $_POST['fast_selling'];
            $data['living_service_furniture'] = $_POST['living_service_furniture'];
            $data['medical_care'] = $_POST['medical_care'];
            $data['other'] = $_POST['other'];
            $data['time'] = $today;
            $industry_proportion->where("id=1")->update($data);
        }
        echo json_encode($data,JSON_UNESCAPED_UNICODE);
    }

    // 合作伙伴及工具数记录
    public function show_add_data() {
        $show_add_data = Db::name('show_add_data');
        $count = $show_add_data->count();
        $res = $show_add_data->order('id desc')->paginate(30,$count);
        // 获取分页显示
        $page = $res->render();
        // 模板变量赋值
        $this->assign('res', $res);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch('show_add_data');
    }

    //登录记录
    public function record_login(){
        $rl = Db::name('record_login');
        $count = $rl->count();
        // 查询状态为1的用户数据 并且每页显示10条数据 总记录数为1000
        $res = $rl->order('id desc')->paginate(30,$count);
        // 获取分页显示
        $page = $res->render();
        // 模板变量赋值
        $this->assign('res', $res);
        $this->assign('page', $page);
        // 渲染模板输出
        return $this->fetch('record_login');
    }
}
